home *** CD-ROM | disk | FTP | other *** search
/ AGA Toolkit '97 / The AGA Toolkit '97.iso / programming / gcc / gcc2.7.0 / gcc270cp.lha / gnu / lib / g++-include / std / complext.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-28  |  10.3 KB  |  311 lines

  1. // The template and inlines for the -*- C++ -*- complex number classes.
  2. // Copyright (C) 1994 Free Software Foundation
  3.  
  4. // This file is part of the GNU ANSI C++ Library.  This library is free
  5. // software; you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software
  7. // Foundation; either version 2, or (at your option) any later version.
  8.  
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. // GNU General Public License for more details.
  13.  
  14. // You should have received a copy of the GNU General Public License
  15. // along with this library; see the file COPYING.  If not, write to the Free
  16. // Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  
  18. // As a special exception, if you link this library with files compiled
  19. // with a GNU compiler to produce an executable, this does not cause the
  20. // resulting executable to be covered by the GNU General Public License.
  21. // This exception does not however invalidate any other reasons why the
  22. // executable file might be covered by the GNU General Public License.
  23.  
  24. // Written by Jason Merrill based upon the specification in the 27 May 1994
  25. // C++ working paper, ANSI document X3J16/94-0098.
  26.  
  27. #ifdef __GNUG__
  28. #pragma interface
  29. #endif
  30.  
  31. #include <std/cmath.h>
  32.  
  33. #if ! defined (__GNUG__) && ! defined (__attribute__)
  34. #define __attribute__ (foo) /* Ignore.  */
  35. #endif
  36.  
  37. template <class FLOAT>
  38. class complex
  39. {
  40. public:
  41.   complex (FLOAT r = 0, FLOAT i = 0): re (r), im (i) { }
  42.   complex& operator += (const complex&);
  43.   complex& operator -= (const complex&);
  44.   complex& operator *= (const complex&);
  45.   complex& operator /= (const complex&);
  46.   FLOAT real () const { return re; }
  47.   FLOAT imag () const { return im; }
  48. private:
  49.   FLOAT re, im;
  50.  
  51.   // These functions are specified as friends for purposes of name injection;
  52.   // they do not actually reference private members.
  53.   friend FLOAT real (const complex&) __attribute__ ((const));
  54.   friend FLOAT imag (const complex&) __attribute__ ((const));
  55.   friend complex operator + (const complex&, const complex&) __attribute__ ((const));
  56.   friend complex operator + (const complex&, FLOAT) __attribute__ ((const));
  57.   friend complex operator + (FLOAT, const complex&) __attribute__ ((const));
  58.   friend complex operator - (const complex&, const complex&) __attribute__ ((const));
  59.   friend complex operator - (const complex&, FLOAT) __attribute__ ((const));
  60.   friend complex operator - (FLOAT, const complex&) __attribute__ ((const));
  61.   friend complex operator * (const complex&, const complex&) __attribute__ ((const));
  62.   friend complex operator * (const complex&, FLOAT) __attribute__ ((const));
  63.   friend complex operator * (FLOAT, const complex&) __attribute__ ((const));
  64.   friend complex operator / (const complex&, const complex&) __attribute__ ((const));
  65.   friend complex operator / (const complex&, FLOAT) __attribute__ ((const));
  66.   friend complex operator / (FLOAT, const complex&) __attribute__ ((const));
  67.   friend bool operator == (const complex&, const complex&) __attribute__ ((const));
  68.   friend bool operator == (const complex&, FLOAT) __attribute__ ((const));
  69.   friend bool operator == (FLOAT, const complex&) __attribute__ ((const));
  70.   friend bool operator != (const complex&, const complex&) __attribute__ ((const));
  71.   friend bool operator != (const complex&, FLOAT) __attribute__ ((const));
  72.   friend bool operator != (FLOAT, const complex&) __attribute__ ((const));
  73.   friend complex polar (FLOAT, FLOAT) __attribute__ ((const));
  74.   friend complex pow (const complex&, const complex&) __attribute__ ((const));
  75.   friend complex pow (const complex&, FLOAT) __attribute__ ((const));
  76.   friend complex pow (const complex&, int) __attribute__ ((const));
  77.   friend complex pow (FLOAT, const complex&) __attribute__ ((const));
  78.   friend istream& operator>> (istream&, complex&);
  79.   friend ostream& operator<< (ostream&, const complex&);
  80. };
  81.  
  82. // Declare specializations.
  83. class complex<float>;
  84. class complex<double>;
  85. class complex<long double>;
  86.  
  87. template <class FLOAT>
  88. inline complex<FLOAT>&
  89. complex<FLOAT>::operator += (const complex<FLOAT>& r)
  90. {
  91.   re += r.re;
  92.   im += r.im;
  93.   return *this;
  94. }
  95.  
  96. template <class FLOAT>
  97. inline complex<FLOAT>&
  98. complex<FLOAT>::operator -= (const complex<FLOAT>& r)
  99. {
  100.   re -= r.re;
  101.   im -= r.im;
  102.   return *this;
  103. }
  104.  
  105. template <class FLOAT>
  106. inline complex<FLOAT>&
  107. complex<FLOAT>::operator *= (const complex<FLOAT>& r)
  108. {
  109.   FLOAT f = re * r.re - im * r.im;
  110.   im = re * r.im + im * r.re;
  111.   re = f;
  112.   return *this;
  113. }
  114.  
  115. template <class FLOAT> inline FLOAT
  116. imag (const complex<FLOAT>& x) __attribute__ ((const))
  117. {
  118.   return x.imag ();
  119. }
  120.  
  121. template <class FLOAT> inline FLOAT
  122. real (const complex<FLOAT>& x) __attribute__ ((const))
  123. {
  124.   return x.real ();
  125. }
  126.  
  127. template <class FLOAT> inline complex<FLOAT>
  128. operator + (const complex<FLOAT>& x, const complex<FLOAT>& y) __attribute__ ((const))
  129. {
  130.   return complex<FLOAT> (real (x) + real (y), imag (x) + imag (y));
  131. }
  132.  
  133. template <class FLOAT> inline complex<FLOAT>
  134. operator + (const complex<FLOAT>& x, FLOAT y) __attribute__ ((const))
  135. {
  136.   return complex<FLOAT> (real (x) + y, imag (x));
  137. }
  138.  
  139. template <class FLOAT> inline complex<FLOAT>
  140. operator + (FLOAT x, const complex<FLOAT>& y) __attribute__ ((const))
  141. {
  142.   return complex<FLOAT> (x + real (y), imag (y));
  143. }
  144.  
  145. template <class FLOAT> inline complex<FLOAT>
  146. operator - (const complex<FLOAT>& x, const complex<FLOAT>& y) __attribute__ ((const))
  147. {
  148.   return complex<FLOAT> (real (x) - real (y), imag (x) - imag (y));
  149. }
  150.  
  151. template <class FLOAT> inline complex<FLOAT>
  152. operator - (const complex<FLOAT>& x, FLOAT y) __attribute__ ((const))
  153. {
  154.   return complex<FLOAT> (real (x) - y, imag (x));
  155. }
  156.  
  157. template <class FLOAT> inline complex<FLOAT>
  158. operator - (FLOAT x, const complex<FLOAT>& y) __attribute__ ((const))
  159. {
  160.   return complex<FLOAT> (x - real (y), - imag (y));
  161. }
  162.  
  163. template <class FLOAT> inline complex<FLOAT>
  164. operator * (const complex<FLOAT>& x, const complex<FLOAT>& y) __attribute__ ((const))
  165. {
  166.   return complex<FLOAT> (real (x) * real (y) - imag (x) * imag (y),
  167.                real (x) * imag (y) + imag (x) * real (y));
  168. }
  169.  
  170. template <class FLOAT> inline complex<FLOAT>
  171. operator * (const complex<FLOAT>& x, FLOAT y) __attribute__ ((const))
  172. {
  173.   return complex<FLOAT> (real (x) * y, imag (x) * y);
  174. }
  175.  
  176. template <class FLOAT> inline complex<FLOAT>
  177. operator * (FLOAT x, const complex<FLOAT>& y) __attribute__ ((const))
  178. {
  179.   return complex<FLOAT> (x * real (y), x * imag (y));
  180. }
  181.  
  182. template <class FLOAT> complex<FLOAT>
  183. operator / (const complex<FLOAT>& x, FLOAT y) __attribute__ ((const))
  184. {
  185.   return complex<FLOAT> (real (x) / y, imag (x) / y);
  186. }
  187.  
  188. template <class FLOAT> inline complex<FLOAT>
  189. operator + (const complex<FLOAT>& x) __attribute__ ((const))
  190. {
  191.   return x;
  192. }
  193.  
  194. template <class FLOAT> inline complex<FLOAT>
  195. operator - (const complex<FLOAT>& x) __attribute__ ((const))
  196. {
  197.   return complex<FLOAT> (-real (x), -imag (x));
  198. }
  199.  
  200. template <class FLOAT> inline bool
  201. operator == (const complex<FLOAT>& x, const complex<FLOAT>& y) __attribute__ ((const))
  202. {
  203.   return real (x) == real (y) && imag (x) == imag (y);
  204. }
  205.  
  206. template <class FLOAT> inline bool
  207. operator == (const complex<FLOAT>& x, FLOAT y) __attribute__ ((const))
  208. {
  209.   return real (x) == y && imag (x) == 0;
  210. }
  211.  
  212. template <class FLOAT> inline bool
  213. operator == (FLOAT x, const complex<FLOAT>& y) __attribute__ ((const))
  214. {
  215.   return x == real (y) && imag (y) == 0;
  216. }
  217.  
  218. template <class FLOAT> inline bool
  219. operator != (const complex<FLOAT>& x, const complex<FLOAT>& y) __attribute__ ((const))
  220. {
  221.   return real (x) != real (y) || imag (x) != imag (y);
  222. }
  223.  
  224. template <class FLOAT> inline bool
  225. operator != (const complex<FLOAT>& x, FLOAT y) __attribute__ ((const))
  226. {
  227.   return real (x) != y || imag (x) != 0;
  228. }
  229.  
  230. template <class FLOAT> inline bool
  231. operator != (FLOAT x, const complex<FLOAT>& y) __attribute__ ((const))
  232. {
  233.   return x != real (y) || imag (y) != 0;
  234. }
  235.  
  236. // Some targets don't provide a prototype for hypot when -ansi.
  237. extern "C" double hypot (double, double) __attribute__ ((const));
  238.  
  239. template <class FLOAT> inline FLOAT
  240. abs (const complex<FLOAT>& x) __attribute__ ((const))
  241. {
  242.   return hypot (real (x), imag (x));
  243. }
  244.  
  245. template <class FLOAT> inline FLOAT
  246. arg (const complex<FLOAT>& x) __attribute__ ((const))
  247. {
  248.   return atan2 (imag (x), real (x));
  249. }
  250.  
  251. template <class FLOAT> inline complex<FLOAT>
  252. polar (FLOAT r, FLOAT t) __attribute__ ((const))
  253. {
  254.   return complex<FLOAT> (r * cos (t), r * sin (t));
  255. }
  256.  
  257. template <class FLOAT> inline complex<FLOAT>
  258. conj (const complex<FLOAT>& x)  __attribute__ ((const))
  259. {
  260.   return complex<FLOAT> (real (x), -imag (x));
  261. }
  262.  
  263. template <class FLOAT> inline FLOAT
  264. norm (const complex<FLOAT>& x) __attribute__ ((const))
  265. {
  266.   return real (x) * real (x) + imag (x) * imag (x);
  267. }
  268.  
  269. // Declarations of templates in complext.ccI
  270.  
  271. template <class FLOAT> complex<FLOAT>
  272.   operator / (const complex<FLOAT>&, const complex<FLOAT>&) __attribute__ ((const));
  273. template <class FLOAT> complex<FLOAT>
  274.   operator / (FLOAT, const complex<FLOAT>&) __attribute__ ((const));
  275. template <class FLOAT> complex<FLOAT>
  276.   cos (const complex<FLOAT>&) __attribute__ ((const));
  277. template <class FLOAT> complex<FLOAT>
  278.   cosh (const complex<FLOAT>&) __attribute__ ((const));
  279. template <class FLOAT> complex<FLOAT>
  280.   exp (const complex<FLOAT>&) __attribute__ ((const));
  281. template <class FLOAT> complex<FLOAT>
  282.   log (const complex<FLOAT>&) __attribute__ ((const));
  283. template <class FLOAT> complex<FLOAT>
  284.   pow (const complex<FLOAT>&, const complex<FLOAT>&) __attribute__ ((const));
  285. template <class FLOAT> complex<FLOAT>
  286.   pow (const complex<FLOAT>&, FLOAT) __attribute__ ((const));
  287. template <class FLOAT> complex<FLOAT>
  288.   pow (const complex<FLOAT>&, int) __attribute__ ((const));
  289. template <class FLOAT> complex<FLOAT>
  290.   pow (FLOAT, const complex<FLOAT>&) __attribute__ ((const));
  291. template <class FLOAT> complex<FLOAT>
  292.   sin (const complex<FLOAT>&) __attribute__ ((const));
  293. template <class FLOAT> complex<FLOAT>
  294.   sinh (const complex<FLOAT>&) __attribute__ ((const));
  295. template <class FLOAT> complex<FLOAT>
  296.   sqrt (const complex<FLOAT>&) __attribute__ ((const));
  297.  
  298. class istream;
  299. class ostream;
  300. template <class FLOAT> istream& operator >> (istream&, complex<FLOAT>&);
  301. template <class FLOAT> ostream& operator << (ostream&, const complex<FLOAT>&);
  302.  
  303. // Specializations and such
  304.  
  305. #include <std/fcomplex.h>
  306. #include <std/dcomplex.h>
  307. #include <std/ldcomplex.h>
  308.  
  309. // Declare the instantiations.
  310. #include <std/cinst.h>
  311.